package org.demo.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

import javax.servlet.AsyncContext;


public class CanceledCallable implements Callable<Object> {
	private AsyncContext asyncContext = null;
	private Long time = null;
	public CanceledCallable(AsyncContext asyncContext) {
		this.asyncContext = asyncContext;
	}

//	public void run() {
//		try {
//			// 等待十秒钟，以模拟业务方法的执行
//			Thread.sleep(3000);
//			PrintWriter out = ctx.getResponse().getWriter();
//			out.println("业务处理完毕的时间：" + new Date() + ".");
//			out.flush();
//			ctx.complete();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	public CanceledCallable(long time, AsyncContext asyncContext) {
		this.time = time;
		this.asyncContext = asyncContext;
	}

	@Override
	public Object call() throws Exception {
		Thread.sleep(time);
		PrintWriter out = asyncContext.getResponse().getWriter();
		System.out.println("业务处理完毕的时间：" + new Date() + "."+Thread.currentThread().getId());
//		out.println("业务处理完毕的时间：" + new Date() + "."+Thread.currentThread().getId());
		out.flush();
		asyncContext.complete();
		return null;
	}
}